/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormDetCategoria.java
 *
 * Created on 01/12/2010, 02:31:18 AM
 */

package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.Utiles;
import pe.edu.upla.lamerced.bean.Categoria;
import pe.edu.upla.lamerced.bean.Detcategoria;
import pe.edu.upla.lamerced.service.CategoriaService;

/**
 *
 * @author elvis
 */
public class FormDetCategoria extends javax.swing.JInternalFrame {

   private JDesktopPane escritorio;
   private CategoriaService categoriaService;
   private Detcategoria detcategoria;
   private List<Categoria> listaCategorias;

    /** Creates new form FormDetCategoria */
    public FormDetCategoria() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      btnGrabar = new javax.swing.JButton();
      btnCancelar = new javax.swing.JButton();
      txtNombre = new javax.swing.JTextField();
      lblCategoria = new javax.swing.JComboBox();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();

      setClosable(true);
      setTitle("Sub Categoria");

      btnGrabar.setText("Grabar");
      btnGrabar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnGrabarActionPerformed(evt);
         }
      });

      btnCancelar.setText("Cancelar");
      btnCancelar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelarActionPerformed(evt);
         }
      });

      txtNombre.setText("jTextField2");

      lblCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      jLabel1.setText("Categoria");

      jLabel2.setText("Sub Categoria");

      jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
      jLabel3.setText("Equipamiento de Categoria");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addGap(31, 31, 31)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                     .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtNombre))
                     .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(btnGrabar)
                        .addGap(51, 51, 51)
                        .addComponent(btnCancelar))
                     .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(26, 26, 26)
                        .addComponent(lblCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))))
               .addGroup(layout.createSequentialGroup()
                  .addGap(64, 64, 64)
                  .addComponent(jLabel3)))
            .addContainerGap(23, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(lblCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel2)
               .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnCancelar)
               .addComponent(btnGrabar))
            .addContainerGap(24, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
       // TODO add your handling code here:
       //Detcategoria det = new Detcategoria();
       detcategoria.setNombre(txtNombre.getText());
       detcategoria.setCategoria(listaCategorias.get(getLblCategoria().getSelectedIndex()));
       categoriaService.registrarDetalleCategoria(detcategoria);
       JOptionPane.showMessageDialog(this, "Grabado con exito");
       cargaListaForm();
       this.setVisible(false);
    }//GEN-LAST:event_btnGrabarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
       cargaListaForm();
       this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnCancelar;
   private javax.swing.JButton btnGrabar;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JComboBox lblCategoria;
   private javax.swing.JTextField txtNombre;
   // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(categoriaService==null){
         categoriaService = (CategoriaService) Inicial.getFactory().getBean("categoriaService");
      }
      if(listaCategorias==null){
         listaCategorias = categoriaService.listarCategorias();
      }
      getLblCategoria().removeAllItems();
      for(Categoria cat : listaCategorias){
         getLblCategoria().addItem(cat.getNombre());
      }
      txtNombre.setText(Utiles.nullToBlank(detcategoria.getNombre()));
      if(detcategoria.getCategoria() !=null)
      getLblCategoria().setSelectedItem(Utiles.nullToBlank( detcategoria.getCategoria().getNombre() ) );
   }
   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }
   public Detcategoria getDetcategoria() {
      return detcategoria;
   }
   public void setDetcategoria(Detcategoria detcategoria) {
      this.detcategoria = detcategoria;
   }
   public void cargaListaForm(){
      ListaDetalleCategoria form = new ListaDetalleCategoria();
       form.cargainicial();
       form.setEscritorio(escritorio);
       escritorio.add(form);
       form.setVisible(true);
   }

   public javax.swing.JComboBox getLblCategoria() {
      return lblCategoria;
   }
   
}
